package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.SysTeam;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.SysTeamService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/sys/team")
public class SysTeamController {

    @Resource(name = "sysTeamService")
    private SysTeamService sysTeamService;

    @GetMapping
    public Result<Page<SysTeam>> get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum, String name , Integer status) throws IframeException {
        Page<SysTeam> page = Page.of(pageNo, pageNum);
        LambdaQueryWrapper<SysTeam> lambdaQueryWrapper = new LambdaQueryWrapper<SysTeam>();
        if (StringUtils.isNotEmpty(name)) {
            lambdaQueryWrapper.like(SysTeam::getName, name);
        }
        if(status != null){
            lambdaQueryWrapper.eq(SysTeam::getStatus , status);
        }
        return Result.ok(sysTeamService.page(page, lambdaQueryWrapper));
    }

    @GetMapping("/{id}")
    public Result<SysTeam> get(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysTeamService.getById(id));
    }

    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody SysTeam sysTeam) throws IframeException {
        return Result.ok(sysTeamService.save(sysTeam));
    }

    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody SysTeam sysTeam) throws IframeException {
        return Result.ok(sysTeamService.update(sysTeam));
    }

    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysTeamService.delete(id));
    }

    @GetMapping("/status")
    public Result<Boolean> status(@RequestParam("id") Integer id, @RequestParam("status") Integer status) throws IframeException {
        SysTeam sysTeam = new SysTeam();
        sysTeam.setId(id);
        sysTeam.setStatus(status);
        return Result.ok(sysTeamService.updateById(sysTeam));
    }
}
